package com.oristand.sqlmonitor;

import com.oristand.sqlmonitor.config.DynamicDatasource;
import com.oristand.sqlmonitor.entity.DbEntity;
import com.oristand.sqlmonitor.mapper.DbMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.BeansException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.List;

@SpringBootApplication
//@MapperScan("com.oristand.sqlmonitor.mapper")
public class SqlMonitorApplication implements ApplicationRunner, ApplicationContextAware {


    private ApplicationContext applicationContext;


    public static void main(String[] args) {
        SpringApplication.run(SqlMonitorApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<DbEntity> list = applicationContext.getBean(DbMapper.class).listAll();
        applicationContext.getBean(DynamicDatasource.class).addDataSourceBeans(list);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }


}
